Skip to content

Conversation

@DavidMStraub
Copy link
Member

@DavidMStraub DavidMStraub commented Sep 27, 2025

Typing of functions/methods involving Gramps objects has become significantly more complicated in Gramps 6.0 due to the introduction of DataDict.

As was discussed in various places, e.g. here, protocols seem to be the right way to address this.

This PR contains a proof of concept, adding protocols for all primary the Person primary type and some secondary Gramps types. The code at the bottom checks that the instances are compatible with the protocols (at list w.r.t. existence of methods).

Related: #2018, #1934, #1919, #2010.

CC @dsblank, @stevenyoungs, @bartfeenstra


@runtime_checkable
class GrampsTypeLike(Protocol):
"""Protocol for GrampsType-like objects (NameType, AttributeType, etc.)."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If protocols are used, they should be the 'primary' types. In other words, concrete types should refer to the protocols, and not the other way around, so a GrampsType is defined as a GrampsTypeLike, but a GrampsTypeLike is not defined as a GrampsType-like.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree actually - unless I'm misunderstanding what you mean. The whole point of protocols is that they can be used without inheritance. For instance, DataDict cannot inherit from PersonLike because not every DataDict has a surname_list (it might be a family or an event etc.).

It's like with database proxies in Gramps that sometimes don't inherit from DbGeneric but just share a subset of its methods.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If protocols are used like interfaces (as in the abstract programming concepts), such interfaces should not reference concrete types. For example, a 'person-like' interface is not a 'person' (concrete type). Instead, it's the other way around: a 'person' concrete type is a 'person-like' interface. That way other code can type on the interface (the protocol, in Python terminology), instead of any concrete type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Anything in the code or in what I said that goes against that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the docstrings, I believe. I wasn't quite clear about that

In essence, with my limited knowledge of Gramps internals, this looks like a good improvement. Are there plans to introduce additional concrete implementations in the future?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happy to complete it for all the other primary types so we can make use of it in other PRs if the feedback is positive.

@dsblank
Copy link
Member

dsblank commented Sep 28, 2025

This looks like a viable path forward to me. Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants